package cn.lovezaoan.hpcenter.parse
{
    import flash.events.Event;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.xml.XMLDocument;
    import mx.collections.ArrayList;
    import mx.collections.XMLListCollection;
    
    /**
     * 提取配置管理类。
     * @author Administrator
     *
     */
    public class ExtractCfgManager
    {
        
        public static var EXTRACT_CFG_FILE:String = "extractCfg.xml";
        
        private static var extractCfgManager:ExtractCfgManager;
        
        private var extractCfgsXML:XML;
        
        private var _extractTypeCfgs:ArrayList;
        
        public static function getExtractCfgManager():ExtractCfgManager
        {
            if (extractCfgManager == null)
            {
                extractCfgManager = new ExtractCfgManager();
            }
            
            extractCfgManager.init();
            
            return extractCfgManager;
        }
        
        public function ExtractCfgManager()
        {
        }
        
        /**
         * 根据类型id获取配置列表。
         * @param typeId
         * @return 返回ExtractType的列表。
         *
         */
        public function getCfgByType(typeId:int):ExtractTypeCfg
        {
            if (extractTypeCfgs != null && extractTypeCfgs.length > 0)
            {
                var typeCfg:ExtractTypeCfg;
                for (var i:int = 0; i < extractTypeCfgs.length; i++)
                {
                    typeCfg = extractTypeCfgs.getItemAt(i) as ExtractTypeCfg;
                    if (typeCfg.id == typeId)
                    {
                        return typeCfg;
                    }
                }
            }
            
            return null;
        }
        
        public function getExctractCfgs():ArrayList
        {
            return extractTypeCfgs;
        }
        
        protected function init():void
        {
            readXMLCfgFile();
            
            if (extractCfgsXML != null && extractCfgsXML.length() > 0)
            {
                extractTypeCfgs = new ArrayList();
                var typeCfg:ExtractTypeCfg = null;
                var type:XML = null;
                var sites:XMLList = null;
                var siteCfg:ExtractSiteCfg = null;
                var siteCfgs:ArrayList;
                var siteProAndRule:Array;
                var msgRules:XMLList;
                var pro:String;
                var rule:String;
                for (var i:int = 0; i < extractCfgsXML.children().length(); i++)
                {
                    typeCfg = new ExtractTypeCfg();
                    type = extractCfgsXML.type[i];
                    typeCfg.id = type.id;
					typeCfg.parentId = type.parentid;
					typeCfg.name = type.name;
					typeCfg.displayType = type.displaytype;
                    // 设置类型对应的多个网站的配置
                    sites = type.sites;
                    if (sites != null && sites.length() > 0)
                    {
                        typeCfg.siteCfgs = new ArrayList();
                        for (var j:int = 0; j < sites.children().length(); j++)
                        {
                            siteCfg = new ExtractSiteCfg();
                            siteCfg.name = sites.site[j].name;
                            siteCfg.url = sites.site[j].url;
                            // 设置网站要提取的数据的每个元数据对应的规则
                            msgRules = sites.site[j].message;
                            if (msgRules != null && msgRules.length() > 0)
                            {
                                var pros:XMLList = msgRules[0].properties;
                                if (pros != null && pros.length() > 0)
                                {
                                    siteCfg.propertyNames = new ArrayList();
                                    siteCfg.propertyRules = new ArrayList();
                                    for (var k:int = 0; k < pros.children().length(); k++)
                                    {
                                        siteCfg.propertyNames.addItem(pros.property[k].name.toString());
                                        siteCfg.propertyRules.addItem(pros.property[k].rule.toString());
                                    }
                                }
                                
                                
                                
                                
                            }
                            
                            typeCfg.siteCfgs.addItem(siteCfg);
                        }
                    }
                    extractTypeCfgs.addItem(typeCfg);
                }
            }
            
            trace(extractCfgsXML.length());
        }
        
        private function readXMLCfgFile():void
        {
            var f:File = new File();
            f = File.applicationDirectory; // 必须先new一个File，File.applicationDirectory才有值，原因不明
            var file:File = new File(f.nativePath + "/cn/lovezaoan/hpcenter/parse/extractCfg.xml");
            var fileStream:FileStream = new FileStream();
            fileStream.open(file, FileMode.READ);
            extractCfgsXML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
            fileStream.close();
        }

		public function get extractTypeCfgs():ArrayList
		{
			return _extractTypeCfgs;
		}

		public function set extractTypeCfgs(value:ArrayList):void
		{
			_extractTypeCfgs = value;
		}

    }
}